import {getStorage, setStorage} from '../core/storage';
let ble;

/*
const PROTOCOL = {
	serviceUUID:"6E40FC00-B5A3-F393-E0A9-E50E24DCCA9E",
	characteristic:"6E40FC20-B5A3-F393-E0A9-E50E24DCCA9E"
}
*/

const STATE = {
	ON:         "poweredOn",        //设备开启状态 -- 可用状态
	OFF:        "poweredOff",       //设备关闭状态
	RESET:       "resetting",       //正在重置状态
	UNAUTH:      "unauthorized",    //设备未授权状态
	UNKNOWN:     "unknown",         //初始的时候是未知的
	UNSUPPORTED: "unsupported"      //设备不支持的状态
}

export function init(){
	ble = ble || api.require("ble");
	let promise = new Promise(function(resolve,reject){
		ble.initManager(function(ret){
			switch(ret.state){
				case STATE.ON:
				  resolve({status:1,msg:"蓝牙开启状态"});
				  break;
				case STATE.OFF:
    	    reject({status:0,msg:"蓝牙关闭状态"});
    	    break;
    	  case STATE.RESET:
    	    reject({status:0,msg:"蓝牙正在重置状态"});
    	    break;
    	  case STATE.UNAUTH:
    	    reject({status:0,msg:"蓝牙未授权状态"});
    	    break;
				default:
				  reject({status:0,msg:"设备不支持"});
				  break;
			}
		});
	});
	return promise;
}

export function scan(){
	ble = ble || api.require("ble");

	let promise = new Promise(function(){
		if(!ble) throw new Error("没有获取到蓝牙模块");
		console.log("awwraewr");

		setStorage("person",{name:"vincent",age:30});
		getStorage("person");

	});

	return promise;

}